<?php

namespace catchAdmin\settlement\controller;

use catcher\base\CatchRequest as Request;
use catcher\CatchAuth;
use catcher\CatchResponse;
use catcher\base\CatchController;
use catchAdmin\settlement\model\Bill as BillModel;
use think\facade\Db;
use think\Response;

class Bill extends CatchController
{
    
    protected $billModel;
    
    /**
     *
     * @time 2022/03/10 14:56
     * @param BillModel $billModel
     * @return mixed
     */
    public function __construct(BillModel $billModel)
    {
        $this->billModel = $billModel;
    }

    /**
     * 列表
     * @param CatchAuth $auth
     * @return Response
     */
    public function index(CatchAuth $auth) : Response
    {
        $user=$auth->user();
        return CatchResponse::paginate($this->billModel->getList($user['tenant_id']));
    }

    /**
     * 新建
     * @param CatchAuth $auth
     * @param Request $request
     * @return Response
     */
    public function create(CatchAuth $auth,Request $request):Response
    {
        $user=$auth->user();
        $data=$request->filterEmptyField()->post();
        $data['tenant_id']=$user['tenant_id'];
        $this->billModel->storeBy($data);
        return CatchResponse::success('','新建成功');
    }
    
    /**
     * 更新
     * @time 2022/03/10 14:56
     * @param Request $request
     * @return Response
     */
    public function update(Request $request) : Response
    {
        $this->billModel->updateBy($request->filterEmptyField()->post('id'),$request->filterEmptyField()->post());
        return CatchResponse::success('','更新成功');
    }

    /**
     * @param Request $request
     * @return Response
     */
    public function delete(Request $request) : Response
    {
        $id=$request->filterEmptyField()->post(['id']);
        $this->billModel->deleteBy($id);
        return CatchResponse::success('','删除成功');
    }
    /**
     * 打印
     * @time 2022/04/08 15:16
     * @param Request $request
     * @return \think\response\Json
     */
    public function print(Request $request)
    {
        $id = $request->post('id');
        $where = [];
        $where['a.id'] = $id;
        $data = DB::name('db_bill')->alias('a')
            ->field('a.*,b.entrust_date,b.volume,b.inside_type,b.main_doc_no,b.sub_order_No,b.entrust_company')
            ->join('db_entrust_order b','a.job_number=b.job_number')
            ->where($where)
            ->find();
        $data['created_at'] = date('Y-m-d H:m:s',$data['created_at']);
        $user = DB::name('users')->where('id',$data['creator_id'])->find();
        $data['creator'] = $user['username'];
        $data['merchant_name'] = $user['merchant_name'];
        //$data['entrust_company_info'] = DB::name('db_provider')->where('id',$data['entrust_company'])->find();
        $data['inside_type_name'] = DB::name('db_inside_type')->where('id',$data['inside_type'])->value('name');
        return CatchResponse::success($data, '查询成功');
    }
}